package com.example.farmeri.common;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.example.farmeri.tools.ResponseUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Create by weiqishan on 2020/5/20.
 */
public class MyCallback implements Callback {


    @Override
    public void onFailure(Call call, IOException e) {
        Log.e("Callback", "onFailure: " + e.getMessage());
        EventBus.getDefault().post("网络请求失败");
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        int code = response.code();
        Log.d("Callback", "onResponse: code = " + code);
        String body = response.body().string();
        Log.d("Callback", "onResponse: body =  " + body);
        if (code == 200){
            ResponseUtils responseUtils =  JSON.parseObject(body, ResponseUtils.class);
            EventBus.getDefault().post(responseUtils);
        }
    }
}
